﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gollie.DataTables;

namespace Gollie.Controls
{
	public partial class VendorTransactions : AuthenticationRequiredAdministratorBaseUserControl
	{
		private DateTime from;
		private DateTime to;

		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected override void OnPostBack(object sender, EventArgs e)
		{
			this.Session.Remove("vendortransactions");

			this.Refresh();
		}

		protected override void OnFirstHit(object sender, EventArgs e)
		{
			this.Session.Remove("vendortransactions");

			this.Initialize();

			this.Refresh();
		}

		private void Initialize()
		{
			DateTime toDay = DateTime.Today;
			DateTime weekEarlier = toDay.AddDays(-7);
			this.tbDateFrom.Text = ObjectFormatter.GetShortDateString(weekEarlier);
			this.tbDateTo.Text = ObjectFormatter.GetShortDateString(DateTime.Today);
		}

		private void Refresh()
		{
			bool ok = this.ValidatePeriod();

			if (!ok)
				return;

			DataTables.VendorTransactions transactions = this.Transactions;

			this.GraphControl.FillGraph(transactions);

			decimal totTransactions = transactions.Sum();

			tbTotTransactions.Value = ObjectFormatter.GetCurrencyString(totTransactions);

			this.GridView1.DataSource = transactions;

			this.GridView1.DataBind();
		}

		private bool ValidatePeriod()
		{
			bool ok = true;

			if (DateTime.TryParse(this.tbDateFrom.Text, out from) == false)
			{
				ok = false;
				this.tbDateFrom.BackColor = DashBoard.errorColor;
			}
			else
				this.tbDateFrom.BackColor = System.Drawing.Color.White;

			if (DateTime.TryParse(this.tbDateTo.Text, out to) == false)
			{
				ok = false;
				this.tbDateTo.BackColor = DashBoard.errorColor;
			}
			else
				this.tbDateTo.BackColor = System.Drawing.Color.White;

			if (from > to)
			{
				ok = false;
				this.tbDateFrom.BackColor = DashBoard.errorColor;
				this.tbDateTo.BackColor = DashBoard.errorColor;
			}

			return ok;
		}

		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				string sDag = GridView1.DataKeys[e.Row.RowIndex].Value.ToString();

				TransactionHistory t = this.Transactions.Transactions[Convert.ToDateTime(sDag)];

				if (t.Total == 0)
				{
					Control expandCollapseImage = e.Row.FindControl("pnlExpandCollapseImage");
					if (expandCollapseImage != null)
						expandCollapseImage.Visible = false;
				}
				else
				{
					GridView grdTransactions = (GridView)e.Row.FindControl("grdViewTransactions");
					grdTransactions.DataSource = t;
					grdTransactions.DataBind();
				}
			}
		}

		private DataTables.VendorTransactions Transactions
		{
			get
			{
				DataTables.VendorTransactions transactions = (DataTables.VendorTransactions)Session["vendortransactions"];
				if (transactions == null)
				{
					transactions = new DataTables.VendorTransactions(this.UserInfo.VendorId, this.from, this.to);
					Session["vendortransactions"] = transactions;
				}

				return transactions;
			}
		}

		protected void GridView1_DataBinding(object sender, EventArgs e)
		{
			this.GridView1.Columns[1].FooterText = tbTotTransactions.Value;
		}

		protected SortDirection SortDir
		{
			get
			{
				if (ViewState["dirState"] == null)
					ViewState["dirState"] = SortDirection.Ascending;

				return (SortDirection)ViewState["dirState"];
			}
			set
			{
				ViewState["dirState"] = value;
			}
		}

		protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
		{
			bool ok = this.ValidatePeriod();

			if (!ok)
				return;

			SortDir = SortDir == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;
			List<VendorTransactionsItem> list = this.Transactions.Sort(e.SortExpression, SortDir == SortDirection.Ascending);
			this.GridView1.DataSource = list;
			this.GridView1.DataBind();
		}
	}
}